//
//  SandBoxManger.h
//  singleTarget
//
//  Created by   李小云 on 17/9/20.
//  Copyright © 2017年   李小云. All rights reserved.
//

#ifndef SandBoxManger_h
#define SandBoxManger_h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

/**
 *  创建一个文件夹
 *
 *  @param folder 文件夹得路径
 *
 *  @return 是否成功
 */
NS_INLINE BOOL CreateFolder(NSString *folder)
{
    if (folder == nil ) {
        return NO;
    }
    NSFileManager *fileManger = [NSFileManager defaultManager];
    
    BOOL isDir;
    
    if (![fileManger fileExistsAtPath:folder isDirectory:&isDir] && !isDir) {
        
        NSError *error = NULL;
        if ([fileManger createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:&error]) {
            
            if (error) {
                return NO;
            }
            return YES;
        }
    }
    return YES;
}

/**
 *  创建一个文件
 *
 *  @param folder 文件得路径
 *
 *  @return 是否成功
 */

NS_INLINE BOOL CreateFile(NSString *filePath)
{
    if (filePath == nil || [filePath pathExtension]) {
        
        return NO;
    }
    NSFileManager *fileManger = [NSFileManager defaultManager];
    
    BOOL isDir;
    
    if (![fileManger fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) {

        if ([fileManger createFileAtPath:filePath contents:nil attributes:nil]) {
          
            return YES;
            
        }else{
            
            return NO;
        }
    }
    return YES;
}

/**
 *  删除一个文件
 *
 *  @param filePath 文件路径
 */
NS_INLINE void removefile(NSString *filePath){
    
    if (filePath == nil) {
        return;
    }
    NSFileManager *fileManger = [NSFileManager defaultManager];
    if ([fileManger fileExistsAtPath:filePath]) {
        [fileManger removeItemAtPath:filePath error:nil];
    }
}
/**
 *  删除一个目录下的所有文件
 *
 *  @param flodPath 目录路径
 */

NS_INLINE void removefolderAllFile(NSString *flodPath){
    
    if (flodPath == nil) {
         NSLog(@"你输入的目录路径为空");
    }
   
    NSFileManager *fileManger = [NSFileManager defaultManager];
    BOOL isDir = NO;
    
    BOOL isExit = [fileManger fileExistsAtPath:flodPath isDirectory:&isDir];
    //判断是否存在
    if (isExit) {
        //判断是不是目录
        if (isDir) {
            NSArray *dirArray = [fileManger contentsOfDirectoryAtPath:flodPath error:nil];
            NSString *subPath = nil;
            for (NSString *str in dirArray) {
                subPath = [flodPath stringByAppendingPathComponent:str];
                BOOL isSubDir = NO;
                [fileManger fileExistsAtPath:subPath isDirectory:&isSubDir];
                removefolderAllFile(subPath);
            }
            
        }else{
            
            [fileManger removeItemAtPath:flodPath error:nil];
        }
    }else{
        
        NSLog(@"你输入的目录不存在");
    }
    
}


#pragma mark -sandBox path read
/**
 *  NSDocumentDirectory
 *
 *  @return 返回document文件
 */
NS_INLINE NSString *documentDir (void)
{
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    
}
/**
 *  NSCachesDirectory
 *
 *  @return 返回cache文件
 */

NS_INLINE NSString *cacheDir (void)
{
    return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    
}


#define YYCachePathInDocument @"lixiaoyun/YYCache"

NS_INLINE NSString *YYCachePathInSandBox(void)
{
    NSString *YYCachePath = [documentDir() stringByAppendingPathComponent:YYCachePathInDocument];
    if (CreateFolder(YYCachePath)) {
        
        return YYCachePath;
    }
    return @"";
    
}
#define DataTablePathInDocument @"lixiaoyun/DataTable"

NS_INLINE NSString *DataTablePathInSandBox(void)
{
    NSString *YYCachePath = [documentDir() stringByAppendingPathComponent:DataTablePathInDocument];
    if (CreateFolder(YYCachePath)) {
        
        return YYCachePath;
    }
    return @"";
    
}


#pragma mark -sandBox Diskspace
/**
 *  获取总磁盘大小
 *
 *  @return 返回磁盘空间大小byte
 */
NS_INLINE unsigned long long totalDiskSpace(void)
{
    NSDictionary *attrs = [[NSFileManager defaultManager]attributesOfItemAtPath:documentDir() error:nil];
    
    return [attrs[NSFileSystemSize] unsignedLongLongValue];
    
}

/**
 *  获取总磁盘大小
 *
 *  @return 返回磁盘空间大小GB
 */
NS_INLINE unsigned long long totalDiskSpaceWithGB(void)
{
    return totalDiskSpace()/(double)0x40000000;
    
}

/**
 *  获取剩余磁盘大小
 *
 *  @return 返回剩余磁盘空间大小byte
 */
NS_INLINE unsigned long long totalDiskFreeSpace(void)
{
    NSDictionary *attrs = [[NSFileManager defaultManager]attributesOfItemAtPath:documentDir() error:nil];
    
    return [attrs[NSFileSystemFreeSize] unsignedLongLongValue];
    
}


/**
 *  获取总磁盘大小
 *
 *  @return 返回磁盘空间大小GB
 */
NS_INLINE unsigned long long totalDiskFreeSpaceWithGB(void)
{
    return totalDiskFreeSpace()/(double)0x40000000;
    
}
#endif /* SandBoxManger_h */
